<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Services;

use App\Enums\AdminActionTypeEnum;
use App\Models\Enum;

class EnumDbService
{
    public function getIndustry(array $search)
    {
        $builder = Enum::query()->where('type', 1);
        if (isset($search['title'])) {
            $builder->where('title', 'like', '%' . $search['title'] . '%');
            adminLog(new Enum(), AdminActionTypeEnum::SELECT, '查询行业');
        }
        return $builder->get()->toArray();
    }

    public function createIndustry(string $title)
    {
        adminLog(new Enum(), AdminActionTypeEnum::CREATE, '创建行业枚举');
        Enum::query()->create([
            'title' => $title,
            'type' => 1,
        ]);

        return true;
    }

    public function updateIndustry(int $id, string $title)
    {
        /** @var Enum $industry */
        $industry = Enum::query()->find($id);
        $industry->title = $title;
        $industry->save();
        adminLog($industry, AdminActionTypeEnum::UPDATE, '修改行业枚举值');
        return true;
    }

    public function deleteIndustry(int $id)
    {
        $industry = Enum::query()->find($id);
        adminLog($industry, AdminActionTypeEnum::DELETE, '删除行业枚举值');
        $industry->delete();
        return true;
    }
}
